<?php

//好站分类模型
class Classes_model extends Model {

	function Classes_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("site_model", "sm");
		$this->load->model("sitestatis_model", "ssm");
	}
	
	//获取父级分类
	function get_topclassid($classid)
	{
		$row = $this->get($classid);
		return $row["parentid"];
	}
	
	//获取分类子类
	function get_subclass($parentid = 0, $limit = 999)
	{
		$query = $this->db->from("classes")->where(array("parentid"=>$parentid))->limit($limit)->get();
		if($query->num_rows()>0) {
			return $query->result_array();
		}
		return false;
	}
	
	//获取分类信息
	function get($classid)
	{
		$query = $this->db->get_where("classes", array("id"=>$classid));
		if($query->num_rows()>0) {
			return $query->row_array();
		}
		return false;
	}
	
	//获取发现分类数据
	function get_found_class($classid = 0, $limit = 4)
	{
		//获取一级分类
		$topclass = $this->get_subclass($classid);
		if (!$topclass) return false;
		foreach ($topclass as $key => $class) {
			$parentid = $class['id'];
			$class = array();
			$class = $this->get_subclass($parentid, $limit);
			if(!$class) {
				$scount = $this->count_class_site(array($parentid)); 
			} else {
				$allclass = $this->get_subclass($parentid, 999);
				$classids = $this->get_subclassids($allclass);
				$scount = $this->count_class_site($classids); 
			}
			$topclass[$key]['subclass'] = $class;
			$topclass[$key]['scount'] = $scount;
		}
		return $topclass;
	}
	
	//获取子分类ID
	function get_subclassids($classes = array())
	{
		if(!$classes || count($classes) == 0) return false;
		$classids = array();
		foreach ($classes as $class) {
			$classids[] = $class['id'];
		}
		return $classids;
	}
	
	//计算分类小站数量
	function count_class_site($classids = array())
	{
		if(!$classids || count($classids) == 0) return 0;
		//获取子类
		$parentid = $this->get_topclassid($classids[0]);
		if ($parentid == 0) { //顶级分类
			$result = $this->get_subclass($classids[0]);
			$classids = $this->get_subclassids($result);
		} 
		
		$query = $this->db->from("site")->where_in('classid', $classids)->where(array("complete"=>1,"status"=>1))->get();
		if($query->num_rows()>0) {
			return $query->num_rows();
		}
		return 0;
	}
	
	//获取分类小站列表
	function get_class_site($classid, $limit = 2, $offset = 0, $order = "id", $sortby = "desc")
	{
		if(intval($classid) == 0) return false;
		$sites = array();
		$query = $this->db->from("site")->where(array('classid'=>$classid, 'complete'=>1,"status"=>1))->order_by($order, $sortby)->limit($limit, $offset)->get();
		if($query->num_rows()>0) {
			$sites = $query->result_array();
			foreach ($sites as $k => $site) {
				$siteid = $site["id"];
				$sites[$k]['info'] = $this->sm->get_siteinfo($site['id']);
				$sites[$k]['funs'] = $this->ssm->get_sitefuns($siteid);
			}
		}
		return $sites;
	}
	
	//获取分类小站列表+排序
	function get_sort_class_site($classid, $limit = 2, $offset = 0, $sortid = 1, $sortby = "desc")
	{
		if(intval($classid) == 0) return false;
		if(!in_array($sortid, array(1,2))) {
			$order = "siteid";
		} elseif ($sortid == 2) {
			$order = "fans";
		} else {
			$order = "siteid";
		}
		$sites = array();
		$query = $this->db->from("site_statis")->where(array('classid'=>$classid,"status"=>1))->order_by($order, $sortby)->limit($limit, $offset)->get();
		if($query->num_rows()>0) {
			$result = $query->result_array();
			foreach ($result as $k => $row){
				$siteid = $row["siteid"];
				$site = $this->sm->get_site($siteid);
				$sites[$k] = $site;
				$sites[$k]['info'] = $this->sm->get_siteinfo($siteid);
				$sites[$k]['funs'] = $this->ssm->get_sitefuns($siteid);
			}
		}
		return $sites;
	}
	
}

/* End of file */